package com.jojo.component;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class FfmpegComponent{

    //进程对象
    private Process process;

    /**
     * 启动进程
     * @param command
     */
    public void startProcess(String command) {
        Runtime rt = Runtime.getRuntime();
        try {
            //启动进程
            process = rt.exec(command);
            //创建线程用来输出日志信息
            new Thread(() -> {
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
            int exitCode = process.waitFor(); // 等待进程终止
            System.out.println("Process exited with code " + exitCode);
        } catch (InterruptedException | IOException e) {
            killProcess();
            e.printStackTrace();
        }
    }

    /**
     * 停止进程
     */
    public void stopProcess() {
        if (process != null) {
            process.destroy(); // 发送SIGTERM信号，请求进程终止
            try {
                int exitCode = process.waitFor(); // 等待进程终止
                System.out.println("Process exited with code " + exitCode);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt(); // 保留中断状态
                e.printStackTrace();
            }
        }
    }

    /**
     * 进程意外退出时，调用此方法杀死ffmpeg进程
     */
    public void killProcess(){
        try {
            // 在Windows上查找FFmpeg进程的PID
            String os = System.getProperty("os.name").toLowerCase();
            if (os.contains("win")) {
                // Windows系统
                Process process = Runtime.getRuntime().exec("tasklist | findstr ffmpeg");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("ffmpeg.exe")) {
                        String[] parts = line.split("\\s+");
                        String pid = parts[1]; // 假设PID是第二个元素
                        Runtime.getRuntime().exec("taskkill /F /PID " + pid);
                        System.out.println("FFmpeg process with PID " + pid + " has been stopped.");
                        break;
                    }
                }
                reader.close();
            } else {
                // Linux或macOS系统
                Process process = Runtime.getRuntime().exec("ps aux | grep ffmpeg");
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    if (line.contains("ffmpeg")) {
                        String[] parts = line.split("\\s+");
                        String pid = parts[1]; // 假设PID是第二个元素
                        Runtime.getRuntime().exec("kill -9 " + pid);
                        System.out.println("FFmpeg process with PID " + pid + " has been stopped.");
                        break;
                    }
                }
                reader.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isAlive(){
        if (process != null)
            return process.isAlive();
        else
            return false;
    }

}
